<?php

namespace App\Http\Controllers\Client\Activity\Talent;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class WinnerConfirmBeforeController extends ClientController
{

    public function index()
    {
        $all = $this->all;
        $act_id = $all['act_id'];
        $result = [];

        try {
            $activity = DB::table('lottery_activity')->where('act_id', '=', $act_id)->first();
            $result['activity'] = json_decode(json_encode($activity), true);
            // 检查活动状态
            if ($result['activity']['status'] !== Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_PRE) { // 正在开奖
                return $this->setReturnJson(1,'奖励领取已经结束');
            }

            // 检查是否是中奖人
            $participate = DB::table('lottery_users_draw')
                ->where('act_id','=',$act_id)
                ->where('we_id','=',$this->we_id)
                ->first();
            $result['participate'] = json_decode(json_encode($participate), true);
            if($result['participate']['status'] === Data::DATA_DB_LOTTERY_USERS_DRAW_STATUS_OVERTIME){
                return $this->setReturnJson(1,'您未在规定时间内领取，中奖资格已取消，请继续参与其他霸王餐活动~');
            }
            if($result['participate']['status'] !== Data::DATA_DB_LOTTERY_USERS_DRAW_STATUS_WINING){
                return $this->setReturnJson(1,'您的开奖信息有误');
            }
            $result['participate']['cancel_at'] = strtotime($result['participate']['cancel_at']);


            // 查询是否上传了收款二维码
            $collection_qr_code = DB::table('talent')->where('we_id','=',$this->we_id)->value('collection_qr_code');
            $result['collection_qr_code'] = $collection_qr_code && true;


            // 商品详情
            $goods_message = DB::table('lottery_activity as la')
                ->leftJoin('lottery_goods as lg','la.goods_id','=','lg.goods_id')
                ->leftJoin('shop','la.shop_id','=','shop.shop_id')
                ->leftJoin('dict_area as da','shop.district','=','da.id')
                ->where('act_id','=',$act_id)
                ->select('la.goods_id','lg.title_img','lg.goods_name','lg.price','lg.detail','shop.head_img','shop.shop_name','shop.address','da.short_name','shop.contact_man','shop.contact_phone')
                ->first();
            $result['goods_message'] = json_decode(json_encode($goods_message), true);
            $result['goods_message']['head_img'] = AliOss::getUrl($result['goods_message']['head_img']);
            $result['goods_message']['title_img'] = AliOss::getUrl($result['goods_message']['title_img']);


        } catch (\Exception $e) {
            Log::error(WinnerConfirmBeforeController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '查询失败，请稍后重试');
        }
        return $this->setReturnJson(0,'success',$result);
    }

}
